package com.yoomiito.app.ui.search;

import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.model.bean.GoodsInfo;
import com.yoomiito.app.utils.DataUtils;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.Type;
import com.yoomiito.app.utils.UserInfoConfig;
import com.yoomiito.app.widget.CouponView;

import java.util.List;

public class SearchGoodsAdapter extends BaseQuickAdapter<GoodsInfo, BaseViewHolder> {
    public SearchGoodsAdapter(@Nullable List<GoodsInfo> data) {
        super(R.layout.item_goods_1, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, GoodsInfo item) {
        ImageView ivPic = helper.getView(R.id.iv_list);
        TextView titleTv = helper.getView(R.id.tv_list_title);
        CouponView awardView = helper.getView(R.id.awardView);
        CouponView couponView = helper.getView(R.id.couponView);
        TextView priceTv = helper.getView(R.id.tv_voucher_Rear);
        helper.getView(R.id.tv_share_ll).setVisibility(View.GONE);
        helper.getView(R.id.tv_share_ll_store).setVisibility(View.VISIBLE);
        TextView storeNameTv = helper.getView(R.id.tv_share_store_name);
        TextView saleNumTv = helper.getView(R.id.tv_share_saleNum);
        storeNameTv.setText(item.getShopName());
        ImageLoader.getInstance().into(ivPic.getContext(), item.getImageUrl(), ivPic);

        setTitle(titleTv, item.getTitle(), DataUtils.INSTANCE.ivType(Integer.valueOf(item.getSource())));
        if (App.mAppConfig.getIsLogin()&& UserInfoConfig.getRoleId()> Type.LevelType.VIP_4) {
            awardView.setVisibility(SpannableUtils.isEmptyOr0(item.getBuyAwardPrice()) ? View.GONE : View.VISIBLE);
            awardView.setText("¥" + item.getBuyAwardPrice());
        } else {
            awardView.setVisibility(View.GONE);
        }
        boolean showCoupon = SpannableUtils.isEmptyOr0(item.getCouponPrice());
//        couponView.setVisibility(showCoupon ? View.GONE : View.VISIBLE);
        couponView.setHeaderTextVisible(!showCoupon);
        couponView.setText(showCoupon?"特价精选":("¥" + item.getCouponPrice()));

        String goodsPrice = item.getDiscountPrice();
        String oldPrice = item.getZkFinalPrice();
        String price = "¥" + goodsPrice + (SpannableUtils.isEmptyOr0(item.getCouponPrice())?" ":(" ¥" + oldPrice));
        SpannableString spannableString = new SpannableString(price);
        spannableString.setSpan(new RelativeSizeSpan(0.65f), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new RelativeSizeSpan(0.65f), (goodsPrice.contains(".") ? goodsPrice.indexOf(".") : goodsPrice.length()) + 1, price.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_999999)), goodsPrice.length() + 2, price.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
        spannableString.setSpan(strikethroughSpan, goodsPrice.length() + 2, price.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        priceTv.setText(spannableString);
        saleNumTv.setText("月销" + SpannableUtils.saleNum(item.getSalesNum()));
    }


    private void setTitle(TextView tv, String str, int ivResId) {
        tv.setText("");
        SpannableString spanText = new SpannableString(" " + str);
        if (ivResId != 0) {
            Drawable d = mContext.getDrawable(ivResId);
            d.setBounds(0, 0, DensityUtil.dp2px(22), DensityUtil.dp2px(14));
            spanText.setSpan(new ImageSpan(d), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        tv.append(spanText);
    }
}
